//
// Created by 原帅 on 2022/10/7.
//

#include "RLog.h"
#include "stdarg.h"

bool RLog::IsEnableStdout() {
    return RLog::Inst().enableSTDOUT;
}

void RLog::SetEnableStdout(bool enableStdout) {
    RLog::Inst().enableSTDOUT = enableStdout;
}

bool RLog::IsDisbaleLogFile() {
    return RLog::Inst().disbaleLogFile;
}

void RLog::SetDisbaleLogFile(bool disbaleLogFile) {
    RLog::Inst().disbaleLogFile = disbaleLogFile;
}

void RLog::recorderlog(RLogRecorder::RLOGLEVEL rloglevel, std::string log) {
    for (auto lv:vecFilterLevels) {
        if (lv==rloglevel) return;
    }
    if (RLog::Inst().IsEnableStdout()) {
        std::cout << log << std::endl;
    }
    if (RLog::Inst().IsDisbaleLogFile() == false) {
        RLog::Inst().rLogRecorder.recordlog(rloglevel, log);
    }
}

void RLog::RecorderLog(RLogRecorder::RLOGLEVEL rloglevel, const char *format, ...) {
    va_list args;
    va_start(args, format);
    RecorderLog(rloglevel, format, args);
    va_end(args);
}

void RLog::RecorderLog(RLogRecorder::RLOGLEVEL rloglevel, const char *format, va_list args) {
    char msg[RLOG_STACK_BUFF_MAX_SIZE] = {0};
    vsnprintf(msg,RLOG_STACK_BUFF_MAX_SIZE-1,format,args);
    std::string logMsg(msg);
    RLog::Inst().recorderlog(rloglevel,logMsg);
}

RLog &RLog::AddLogLevelFilter(RLogRecorder::RLOGLEVEL rloglevel) {
    for (auto lvl : RLog::Inst().vecFilterLevels)
    {
        if (lvl == rloglevel) return RLog::Inst();
    }
    RLog::Inst().vecFilterLevels.push_back(rloglevel);
    return RLog::Inst();
}
